package com.financing.wap.web.controller.api;


import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.UserAgreeAgreementDTO;
import com.financing.wap.service.IUserAgreeAgreeMentService;
import com.financing.wap.util.StringUtil;
import com.financing.wap.web.controller.BaseController;

/**
 *  
 * @author qiye
 *
 */
@Controller("UserAgreeAgreementController")
@RequestMapping("/api")
public class UserAgreeAgreementController extends BaseController {
	Logger logger = Logger.getLogger(getClass());

	@Autowired
	private IUserAgreeAgreeMentService userAreeAgreementService;
	
	@RequestMapping("/addUserAgreeAgreement")
	@ResponseBody
	public ResultMap addUserAgreeAgreement(HttpServletRequest request, HttpServletResponse response,UserAgreeAgreementDTO dto){
		ResultMap result = new ResultMap();
		try{
			if(StringUtil.isNullOrEmptyStr(dto.getFpFaLoginName()) || StringUtil.isNullOrEmptyStr(dto.getFpAgreeAgreement())){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.ERROR);
				return result;
			}
			
			dto.setFpAgreeTime(new Date());
			dto.setUpdateDate(new Date());
			dto.setCreateDate(new Date());
			if(null == userAreeAgreementService.addUserAgreeAgreement(dto)){
				result.setStatusCode(ResultMap.STATUS_CODE_MOBILE_NUMBER_UNDEFINED);
				result.setStatus(ResultMap.STATUS.ERROR);
			}
			
		}catch(Exception e){
			log.error("addUserAgreeAgreement error!",e);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
			result.setStatus(ResultMap.STATUS.ERROR);
		}
		result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		result.setStatus(ResultMap.STATUS.SUCCESS);
		return result;
	}

	@RequestMapping("/queryAgreeAgreement")
	@ResponseBody
	public ResultMap queryAgreeAgreement(HttpServletRequest request, HttpServletResponse response,String faLoginName){
		ResultMap result = new ResultMap();
		try{
			if(StringUtil.isNullOrEmptyStr(faLoginName)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.ERROR);
				return result;
			}
			
			UserAgreeAgreementDTO dto = userAreeAgreementService.getUserAgreeAgreementByUserLoginName(faLoginName);
			if(null == dto || StringUtil.isNullOrEmptyStr(dto.getFpAgreeAgreement())){
				result.setAttribute("userAgreeAgreement","2" );
			}else{
				result.setAttribute("userAgreeAgreement",dto.getFpAgreeAgreement() );
			}
			
			
		}catch(Exception e){
			log.error("addUserAgreeAgreement error!",e);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
			result.setStatus(ResultMap.STATUS.ERROR);
		}
		result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		result.setStatus(ResultMap.STATUS.SUCCESS);
		return result;
	}

}
